//
//  HMEmoticonKeyboardPageCell.swift
//  68.新工程-表情键盘_界面搭建
//
//  Created by shenzhenIOS on 16/4/20.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

// 1.定义协议
protocol HMEmoticonKeyboardPageCellDelegate: NSObjectProtocol {
    // 2.定义方法
    func emoticonKeyboardPageCell(cell: HMEmoticonKeyboardPageCell, didSelectedEmoticon: HMEmoticonModel)
    
    func emoticonKeyboardPageCellDidClickDeleteButton(cell: HMEmoticonKeyboardPageCell)
}

/// cell和collectionView一样大,里面会有很多表情按钮
class HMEmoticonKeyboardPageCell: UICollectionViewCell {
    
    // 3.代理属性
    weak var delegate: HMEmoticonKeyboardPageCellDelegate?
    
    /// cell要显示20个表情模型,需要这一页的表情模型数组,表情模型数组在表情包里面已经准备好了
    var pageEmoticon: [HMEmoticonModel]? {
        didSet {
            // cell要显示的模型
//            print("cell要显示的一页模型: \(pageEmoticon)")
            
            // 隐藏所有按钮
            for button in emoticonButtons {
                button.hidden = true
            }
            
            // 每页要显示的模型并不一定是20,如果只有2,只会设置2个按钮的内容,其他的将他隐藏起来
            
            // 遍历模型
            for (index, emoticonModel) in pageEmoticon!.enumerate() {
                // 找到模型对应的按钮,将模型里面的内容赋值给按钮
                let emoticonButton = emoticonButtons[index]
                // 要设置模型的按钮才显示出来
                emoticonButton.hidden = false
                
                // 将模型赋值给按钮,按钮自己根据模型来显示
                emoticonButton.emoticonModel = emoticonModel
            }
        }
    }
    
    var indexPath: NSIndexPath? {
        didSet {
            debugLabel.text = "当前第 \(indexPath!.section) 组, 第 \(indexPath!.item) 页"
            
            recentLabel.hidden = indexPath!.section != 0
        }
    }
    
    /// 存放20个表情按钮, 方便访问20个表情按钮
    private var emoticonButtons: [HMEmoticonButton] = [HMEmoticonButton]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        createEmoticons()
        
        // 删除按钮
        contentView.addSubview(deleteButton)
        
        contentView.addSubview(debugLabel)
        debugLabel.frame = bounds
        
        contentView.addSubview(recentLabel)
    }
    
    @objc private func didClickEmoticonButton(button: HMEmoticonButton) {
        // 4.调用代理
        delegate?.emoticonKeyboardPageCell(self, didSelectedEmoticon: button.emoticonModel!)
    }
    
    @objc private func didClickDeleteButton() {
        // 4.调用代理
        delegate?.emoticonKeyboardPageCellDidClickDeleteButton(self)
    }
    
    /// 创建20个表情按钮
    private func createEmoticons() {
        // 0 - 19
        for _ in 0..<20 {
            let button = HMEmoticonButton()
//            button.backgroundColor = UIColor.randomColor()
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            button.addTarget(self, action: "didClickEmoticonButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            emoticonButtons.append(button)
            
            // 添加到ContentView
            contentView.addSubview(button)
        }
    }

    // 设置按钮的位置
    private func layoutButtonFrame() {
        let marginLR: CGFloat = 5
        let marginH: CGFloat = 25
        
        // 一个按钮宽度 = (cell的宽度 - 2 * marginLR) / 按钮列数
        let width = (self.frame.width - 2 * marginLR) / CGFloat(HMEmoticonColumnOfPage)
        
        // 一个按钮宽高度 = (cell的高度 - marginH) / 按钮行数
        let height = (self.frame.height - marginH) / CGFloat(HMEmoticonRowOfPage)
        
        // 设置按钮的frame
        // emoticonButtons.enumerate，不仅会将对应按钮返回给我们,还会将按钮在数组中得位置也返回
        for (index, button) in emoticonButtons.enumerate() {
            // (index: Int, element: UIButton)
            // 当前按钮所在列
            let column = index % HMEmoticonColumnOfPage
            
            // 当前按钮所在行
            let row = index / HMEmoticonColumnOfPage
            
            // x
            let x = marginLR + CGFloat(column) * width
            
            // y
            let y = CGFloat(row) * height
            
            let frame = CGRect(x: x, y: y, width: width, height: height)
            button.frame = frame
        }
        
        // 设置删除按钮的frame
        // 删除按钮的位置 第2行第6列
        let buttonFrame = CGRect(x: marginLR + CGFloat(HMEmoticonColumnOfPage - 1) * width, y: CGFloat(HMEmoticonRowOfPage - 1) * height, width: width, height: height)
        deleteButton.frame = buttonFrame
        
        // recentLabel的frame
        let centerX = (contentView.frame.width - recentLabel.frame.width) * 0.5
        let bottomY = contentView.frame.height - recentLabel.frame.height - 5
        recentLabel.frame.origin = CGPoint(x: centerX, y: bottomY)
    }
    
    override func layoutSubviews() {
         super.layoutSubviews()
        
        // 布局按钮位置
        layoutButtonFrame()
    }
    
    // MARK: - 懒加载
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.redColor()
        
        return label
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
         button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 最近使用label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(12)
        
        label.text = "最近使用表情"
        label.sizeToFit()
        
        return label
    }()
}
